package org.jeecg.modules.common.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class MapBeanUtils {

    /**
     * JavaBean转Map
     * @param obj
     * @return
     */
    public static Map<String, Object> bean2Map(Object obj) {
        Map<String, Object> map = new LinkedHashMap<>();
        Class<?> clazz = obj.getClass();

        //获取到所有属性，包括继承的父类属性
        List<Field> fields = BeanObjUtil.getAllField(clazz);

        for (Field field : fields) {
            field.setAccessible(true);
            String fieldName = field.getName();
            Object value = null;
            try {
                value = field.get(obj);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            //部分对象的数据本身就是null
//            if (value == null){
//                value = null;
//            }
            map.put(fieldName, value);
        }
        return map;
    }

    /**
     * Map转JavaBean
     * @param clazz
     * @param map
     * @param <T>
     * @return
     */
    public static <T> T map2Bean(final Class<T> clazz, final Map<String, ? extends Object> map) {
        if (map == null) {
            return null;
        }
        T res = null;
        try {
            res = clazz.getDeclaredConstructor().newInstance();
            //获取到所有属性，包括继承的父类属性
            List<Field> fields = BeanObjUtil.getAllField(clazz);
            for (Field field : fields) {
                String fieldName = field.getName();
                Object value = map.get(fieldName);
                BeanObjUtil.setFieldValueByName(res,field,value);
//                //获取字段的修饰符
//                int mod = field.getModifiers();
//                if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
//                    continue;
//                }
//                //设置对象的访问权限
//                field.setAccessible(true);
//                //根据属性名称去map获取value
//                if(map.containsKey(field.getName())) {
//                    //给对象赋值
//                    field.set(res, map.get(field.getName()));
//                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }
    /**
     * Map转JavaBean
     * @param clazz
     * @param map
     * @param <T>
     * @return
     */
    public static <Object> Object map2BeanObj( Object obj,  Map<String, ? extends Object> map){
        if (map == null) {
            return null;
        }
        try {
//            res = clazz.getDeclaredConstructor().newInstance();
            //获取到所有属性，包括继承的父类属性
            List<Field> fields = BeanObjUtil.getAllField(obj.getClass());
            for (Field field : fields) {
                String fieldName = field.getName();
                Object value = map.get(fieldName);
                BeanObjUtil.setFieldValueByName(obj,field,value);

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }
    public static void main(String[] args) throws Exception {
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", 1001);
        map.put("username", "zhangsan");
        map.put("password", "123456");
        map.put("nickname", "张三");
        map.put("email", "369950806@qq.com");
        map.put("gender", true);
        map.put("birth", LocalDate.now());
        map.put("avatar", "/aa/bb/ab.jpg");
        map.put("role", "VIP");
        map.put("status", (byte) 1);
        map.put("salt", "ldfkasjghweoiq324");
        map.put("createTime", LocalDateTime.now());
        map.put("updateTime", LocalDateTime.now());

//        User user = map2Bean(User.class, map);
//        System.out.println(user);
//
//        Map<String, Object> res = bean2Map(user);
        System.out.println(map);
    }
}

